home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
HPAVC
/
HPAVC CD-ROM.iso
/
MCASM.RAR
/
MC_ASM.EXE
/
WROX_ASM
/
CH12
/
EFFECTS
/
FADE.C
< prev
next >
Wrap
C/C++ Source or Header
|
1994-04-04
|
4KB
|
185 lines
// This module lets you fadding or better say
// smooth palette transforming (it may be used for
// fade as it is or partial palette changing as well).
// VGA 256 colors mode is assumed.
//
// Compiled under Borland C++ 3.1
// for large model and 286 instruction set
// (c) This program is written by Kiselev J. 1994.
#include <conio.h>
#include <colmodel.h>
#define BYTE unsigned char
#define WORD unsigned int
typedef struct { BYTE R,G,B;} paltype;
typedef struct { float Y,I,Q;} YIQtype;
typedef struct { float H,L,S;} HLStype;
void setDACblock(BYTE,WORD,paltype*);
void fadehls(BYTE,BYTE,BYTE,paltype*,paltype*);
void fadeyiq(BYTE,BYTE,BYTE,paltype*,paltype*);
paltype palette[256];
paltype palette1[256];
WORD i,j;
void main(void)
{
asm {
push di
mov ax,0x13
int 0x10
mov dx,200
mov ax,0xa000
mov es,ax
xor ax,ax
xor di,di
}
nr:
asm {
mov cx,320
rep stosb
inc al
dec dx
jnz nr
pop di
}
// create source palette (redish)
for(i=0;i<256;i++)
{
palette[i].R = 63-i/4;
palette[i].G = 0;
palette[i].B = 0;
}
// create destination palette (greenish)
for(i=0;i<256;i++)
{
palette1[i].R = 0;
palette1[i].G = i/4;
palette1[i].B = 0;
}
fadeyiq(0,255,60,palette,palette1);
// fadehls(0,255,60,palette,palette1);
getch();
}
// the following routine provide smooth palette
// transformation using YIQ color model
// you should input palette range to be transform
// and the number of conversion steps
void fadeyiq(BYTE first,BYTE last,BYTE step,paltype *pal,paltype *pal1)
{
float D,R_,G_,B_,Y,I,Q;
YIQtype YIQ[256];
YIQtype DYIQ[256];
for (j=first;j<=last;j++) // calculate array of increments
{
// for call RGB2YIQ normalyze R,G,B value
RGB2YIQ((float)pal[j].R/63,(float)pal[j].G/63,(float)pal[j].B/63,
YIQ[j].Y,YIQ[j].I,YIQ[j].Q);
RGB2YIQ((float)pal1[j].R/63,(float)pal1[j].G/63,(float)pal1[j].B/63,
Y,I,Q);
DYIQ[j].Y = (YIQ[j].Y - Y)/step;
DYIQ[j].Q = (YIQ[j].Q - Q)/step;
DYIQ[j].I = (YIQ[j].I - I)/step;
}
for (i=1;i<step;i++) // do fade (step - 1) times
{
for (j=first;j<=last;j++) // calculate intermediate palette
{
YIQ[j].I = YIQ[j].I-DYIQ[j].I;
YIQ[j].Y = YIQ[j].Y-DYIQ[j].Y;
YIQ[j].Q = YIQ[j].Q-DYIQ[j].Q;
YIQ2RGB(YIQ[j].Y,YIQ[j].I,YIQ[j].Q,R_,G_,B_);
pal[j].R = R_*63;
pal[j].G = G_*63;
pal[j].B = B_*63;
}
setDACblock(first,(last-first)+1,pal); //set VGA palette
}
// set VGA palette with last palette
setDACblock(first,(last-first)+1,pal1);
}
// the following routine provide smooth palette
// transformation using HLS color model
void fadehls(BYTE first,BYTE last,BYTE step,paltype *pal,paltype *pal1)
{
float D,R_,G_,B_,H,L,S;
HLStype HLS[256];
HLStype DHLS[256];
for (j=first;j<=last;j++) // calculate array of increments
{
// for call RGB2HLS normalyze R,G,B value
RGB2HLS((float)pal[j].R/63,(float)pal[j].G/63,(float)pal[j].B/63,
HLS[j].H,HLS[j].L,HLS[j].S);
RGB2HLS((float)pal1[j].R/63,(float)pal1[j].G/63,(float)pal1[j].B/63,
H,L,S);
// being aware of critical situation with Hue (negative or
// UNDEFINED)
if ((HLS[j].H != -400) && (H != -400))
{
D = HLS[j].H - H;
if (D > 0)
if (D > 180) DHLS[j].H = -(360 - D)/step;
else DHLS[j].H = D/step;
else
if (abs(D) > 180) DHLS[j].H = (360 + D)/step;
else DHLS[j].H = D/step;
}
else
{
DHLS[j].H = 0;
if (HLS[j].H == -400) HLS[j].H = H;
}
DHLS[j].L = (HLS[j].L - L)/step;
DHLS[j].S = (HLS[j].S - S)/step;
}
for (i=1;i<step;i++) // do fade (step - 1) times
{
for (j=first;j<=last;j++) // calculate intermediate palette
{
HLS[j].H = HLS[j].H-DHLS[j].H;
HLS[j].L = HLS[j].L-DHLS[j].L;
HLS[j].S = HLS[j].S-DHLS[j].S;
HLS2RGB(HLS[j].H,HLS[j].L,HLS[j].S,R_,G_,B_);
pal[j].R = R_*63;
pal[j].G = G_*63;
pal[j].B = B_*63;
}
setDACblock(first,(last-first)+1,pal); // set VGA palette
}
// set VGA palette with last palette
setDACblock(first,(last-first)+1,pal1);
}
void setDACblock(BYTE first,WORD count,paltype *pal)
{
asm {
push ds
push si
mov dx,0x3da
}
Wait:
asm { in al,dx
test al,8
jz Wait
cld
mov cx,count
mov ax,cx
shl cx,1
add cx,ax // *3
mov dl,0xc8
mov al,first
out dx,al
inc dx
lds si,pal
rep outsb
pop si
pop ds
}
}